﻿using AuditSample.Model;
using System;
using System.Activities.Tracking;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuditSample.Persistence
{
    public class HistoryTrackingParticipant : TrackingParticipant
    {

        public HistoryTrackingDTO Histotry { get; set; }

        protected override void Track(TrackingRecord record, TimeSpan timeout)
        {
            if (record != null)
            {
                if (record is CustomTrackingRecord)
                {
                    var customRecord = (CustomTrackingRecord)record;
                    if (customRecord.Name.Equals("HistoryTracking", StringComparison.InvariantCultureIgnoreCase))
                    {
                        Histotry = new HistoryTrackingDTO();
                        WorkFlowInstance workflowInstance = (new Model1()).WorkFlowInstances.FirstOrDefault(x => x.Id == record.InstanceId.ToString());
                        if (workflowInstance == null) return;
                        Histotry.Id = Guid.NewGuid().ToString();
                        Histotry.InstanceId = record.InstanceId.ToString();
                        Histotry.RecordNumber = record.RecordNumber.ToString();
                        Histotry.State = customRecord.Data.GetString("State");
                        Histotry.Action = customRecord.Data.GetString("Action");
                        Histotry.Auditer = customRecord.Data.GetString("Auditer");
                        Histotry.Comment = customRecord.Data.GetString("Comment");
                        Histotry.StartDate = workflowInstance.UpdatedDate;
                        Histotry.EndDate = customRecord.Data.GetDate("Date");

                    }
                }
            }
        }
    }
}
